package com.smalls.newvideotwo.base;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.smalls.newvideotwo.NewDTVApp;
import com.smalls.newvideotwo.util.EventUtil;

import org.greenrobot.eventbus.Subscribe;

/**
 * Created by thearyong on 2017/3/1.
 * </p>
 */

public abstract class BaseFragment extends Fragment {
    protected NewDTVApp mAppContext;
    protected Activity mActivity;
    protected View view;
    protected Handler hd; // 专门用于post到主线程
    protected boolean isVisible;//懒加载
    protected View mContentView;

    protected abstract void initView(Bundle bundle);

    protected abstract void processLogic(Bundle bundle);

    protected abstract void setListener();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAppContext = NewDTVApp.getApp();
        hd = new Handler(getActivity().getMainLooper());
        this.mActivity = getActivity();
    }
    protected <VT extends View> VT getViewById(@IdRes int id) {
        return (VT)this.mContentView.findViewById(id);
    }
    public void replaceFragment(@IdRes int resID, Fragment fragment) {
        try {
            if (!getChildFragmentManager().isDestroyed() && !this.mActivity.isFinishing()) {
                getChildFragmentManager().beginTransaction().replace(resID, fragment).commitAllowingStateLoss();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (this.mContentView == null) {
            initView(savedInstanceState);
            setListener();
            lazyLoad();
            processLogic(savedInstanceState);
        } else {
            ViewGroup parent = (ViewGroup) this.mContentView.getParent();
            if (parent != null) {
                parent.removeView(this.mContentView);
            }
        }
        return this.mContentView;
    }

    protected void setContentView(@LayoutRes int layoutResID) {
        this.mContentView = LayoutInflater.from(this.mActivity).inflate(layoutResID, null);
    }
    public void startActivity(Class mClass) {
        startActivity(new Intent(mActivity, mClass));
    }

    protected void showToast(String msg) {
        Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT).show();
    }

    @Subscribe
    public void onEvent(Object obj) {
    }

    @Override
    public void onStart() {
        super.onStart();
        EventUtil.register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventUtil.unregister(this);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            isVisible = true;
            lazyLoad();
        } else {
            isVisible = false;
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = activity;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mActivity = null;
    }

    protected void lazyLoad() {
    }


}
